package vtt.android.cartoon_video;

import vtt.android.library.VttBaseApplication;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseInstallation;
import com.parse.ParseUser;
import com.startapp.android.publish.StartAppSDK;

public class BaseApplication extends VttBaseApplication {
    
    /* TEST KEY */
    private static final String PARSER_PUSH_APPID_TEST = "FLVC00YfGcb4PrcHSBTqRJRvYg1b09uLIYRDS62V";
    private static final String PARSER_PUSH_CLIENTID_TEST = "gE7UmFGOyPj0E4gLtl2Z23PmYYTDteytOSkhlUg8";
    
    public static boolean mIsMainActivityRunning = false;

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onCreate() {
        if (isDebuggable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
        }
        super.onCreate();

        initImageLoader(getApplicationContext());

        // Initialize the Parse SDK.
        initParserPushNotification();
        
        // set false for not show ad when return app
        StartAppSDK.init(this, getString(R.string.startapp_dev_id), getString(R.string.startapp_app_id), false);
    }

    /**
     * 
     */
    private void initParserPushNotification() {
        if(isDebuggable){
            Parse.initialize(this, PARSER_PUSH_APPID_TEST, PARSER_PUSH_CLIENTID_TEST);
            Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
        } else {
            Parse.initialize(this, getString(R.string.parse_app_id), getString(R.string.parse_client_key));
            Parse.setLogLevel(Parse.LOG_LEVEL_NONE);
        }
        
        ParseUser.enableAutomaticUser();
        ParseACL defaultACL = new ParseACL();
          
        // If you would like all objects to be private by default, remove this line.
        defaultACL.setPublicReadAccess(true);
        ParseACL.setDefaultACL(defaultACL, true);
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }

	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this); method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
						.threadPriority(Thread.NORM_PRIORITY - 2)
						.denyCacheImageMultipleSizesInMemory()
						.diskCacheFileNameGenerator(new Md5FileNameGenerator())
						.diskCacheSize(100 * 1024 * 1024) // 50 Mb
						//.memoryCache(new WeakMemoryCache())
						//.memoryCacheSizePercentage(10)
						.tasksProcessingOrder(QueueProcessingType.LIFO)
						//.writeDebugLogs() // Remove for release app
						.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}
	
	@Override
	public void onLowMemory() {
		super.onLowMemory();
		log.d("VTT>>> onLowMemory " + "clearMemoryCache");
		ImageLoader.getInstance().clearMemoryCache();
	}
}
